// pages/assess/assess.js
const defaultAvatarUrl = 'https://img.yzcdn.cn/vant/cat.jpeg';
const util = require("../../utils/request");

import Toast from '@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl: defaultAvatarUrl, // 头像
    orderId: '', // 订单id
    assess: [], // 评价信息
    show: false, // 显示抽屉
    content: '', // 评价内容
    openid: '', // 用于判断是否属于自己的评论，决定是否开放删除按钮
    assessId: 0, // 要删除的评论id
    showDialog: false // 显示删除确认框
  },

  /**
   * 根据订单查询评价信息
   */
  getAssessByOrderId() {
    let orderId = this.data.orderId;
    if(orderId == null) return;
    util.request('/rms/assess/selectAssessByOrderId/' + orderId, 'GET', {}, res => {
        let code = res.data.code;
        if(code == 200) {
            let assess = res.data.data;
            if(assess.length > 0) {
                this.setData({assess: assess});
            }
        }
    });
  },

  /**
   * 添加评论按钮
   */
  addAssess() {
      this.setData({
          show: true
      });
  },

  /**
   * 捕获评论输入框内容
   */
  onContent(event) {
    this.setData({
        content: event.detail
    });
  },

  /**
   * 提交评论
   */
  submitAssess() {
      console.log(this.data.content);
      let assess = {
          orderId: this.data.orderId,
          createBy: this.data.openid,
          content: this.data.content
      };
      util.request('/rms/assess', 'POST', assess, res => {
          let code = res.data.code;
          if(code == 200) {
             Toast('评论成功');
          } else {
            Toast('评论失败');
          }
          this.onClose();
          this.getAssessByOrderId();
      })
  },

  /**
   * 关闭抽屉
   */
  onClose() {
      this.setData({
          show: false,
          content: ''
      });
  },

  /**
   * 删除评论按钮
   */
  deleteAssess(e) {
    this.setData({
        assessId: e.currentTarget.dataset.word,
        showDialog: true
    });
  },

  /**
   * 确认删除
   */
  ensureDelete() {
    let assessId = this.data.assessId;
    if(assessId == 0) return;
    util.request('/rms/assess/' + assessId, 'DELETE', {}, res => {
        let code = res.data.code;
        if(code == 200) {
            Toast('删除成功');
            this.getAssessByOrderId();
        } else {
            Toast('删除失败');
        }
        this.cancelDelete();
    });
  },

  /**
   * 取消删除
   */
  cancelDelete() {
    this.setData({
        assessId: 0,
        showDialog: false
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
        orderId: options.orderId,
        openid: wx.getStorageSync('openid')
    });
    this.getAssessByOrderId();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})